// Package main
// @Author zhongxc
// @Date 2024/5/15 11:14:00
// @Desc
package main

import (
	"errors"
	"fmt"
	cloudauth "github.com/alibabacloud-go/cloudauth-20200618/v3/client"
	openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
	util "github.com/alibabacloud-go/tea-utils/v2/service"
	"github.com/alibabacloud-go/tea/tea"
)

func CreateClient(endpoint *string) (_result *cloudauth.Client, _err error) {
	// 阿里云账号AccessKey拥有所有API的访问权限，建议您使用RAM用户进行API访问或日常运维。
	// 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里，否则可能导致AccessKey泄露，威胁您账号下所有资源的安全。
	// 本示例通过阿里云Credentials工具从环境变量中读取AccessKey，来实现API访问的身份验证。
	// 初始化Credential。
	// credential, _err := credentials.NewCredential(nil)
	// if _err != nil {
	// 	panic(_err)
	// }
	// 初始化Client。
	config := &openapi.Config{
		// 使用Credential配置凭证。
		// Credential: credential,
		AccessKeyId:     tea.String("LTAI5tHzBg6WwssJAx5ZCo3e"),
		AccessKeySecret: tea.String("HVn27k3oGnWBnhnwimyTqjqe76u4eh"),
		Endpoint:        endpoint,
		// 设置HTTP代理。
		// HttpProxy: tea.String("http://xx.xx.xx.xx:xxxx"),
		// 设置HTTPS代理。
		// HttpsProxy: tea.String("https://username:password@xxx.xxx.xxx.xxx:9999"),
	}
	// _result = &cloudauth.Client{}
	_result, _err = cloudauth.NewClient(config)
	return _result, _err
}

func _main() (_err error) {
	request := &cloudauth.InitSmartVerifyRequest{
		// 请输入场景ID。
		SceneId:      tea.Int64(4052001),
		OuterOrderNo: tea.String("664427de321a9651805097"),
		// 证件类型，固定值。
		Mode:     tea.String("MARKET_SAFE"),
		Ocr:      tea.String("F"),
		CertType: tea.String("IDENTITY_CARD"),
		CertName: tea.String("钟晓川"),
		CertNo:   tea.String("500224199005142157"),
		// MetaInfo环境参数，需要通过客户端SDK获取。
		MetaInfo: tea.String("{\"zimVer\":\"3.0.0\",\"appVersion\": \"1\",\"bioMetaInfo\": \"4.1.0:11501568,0\",\n        \"appName\": \"com.aliyun.cloudauth\",\"deviceType\": \"ios\",\"osVersion\": \"iOS 10.3.2\",\"apdidToken\": \"\",\"deviceModel\": \"iPhone9,1\"}"),
		Mobile:   tea.String("15723091175"),
		// Ip:            tea.String("114.xxx.xxx.xxx"),
		// UserId:        tea.String("12345xxxx"),
		// CallbackUrl:   tea.String("https://www.aliyundoc.com"),
		// CallbackToken: tea.String("xxxxxxx")
	}
	response, _err := InitSmartVerifyAutoRoute(request)

	// 不支持服务自动路由。
	// response, _err := InitSmartVerify(tea.String("cloudauth.cn-shanghai.aliyuncs.com"), request)

	if _err != nil {
		return _err
	}

	if response != nil {
		fmt.Println(*response.Body.RequestId)
		fmt.Println(*response.Body.Code)
		fmt.Println(*response.Body.Message)
		fmt.Println(*response.Body.RequestId)
		fmt.Println(*response.Body.ResultObject.CertifyId)
	}

	return _err
}

func InitSmartVerifyAutoRoute(request *cloudauth.InitSmartVerifyRequest) (_result *cloudauth.InitSmartVerifyResponse, _err error) {
	endpoints := []*string{tea.String("cloudauth.cn-shanghai.aliyuncs.com"), tea.String("cloudauth.cn-beijing.aliyuncs.com")}
	var lastResponse *cloudauth.InitSmartVerifyResponse
	for _, endpoint := range endpoints {
		response, _err := InitSmartVerify(endpoint, request)
		if _err != nil {
			var err = &tea.SDKError{}
			var _t *tea.SDKError
			if errors.As(_err, &_t) {
				err = _t
				// 网络异常，切换到下个区域调用。
				if tea.BoolValue(util.EqualString(tea.String("ServiceUnavailable"), err.Code)) {
					continue
				}
			}
			return _result, _err
		}

		if tea.BoolValue(util.EqualString(tea.String("500"), response.Body.Code)) {
			continue
		}

		_result = response
		return _result, _err

	}
	_result = lastResponse
	return _result, _err
}

func InitSmartVerify(endpoint *string, request *cloudauth.InitSmartVerifyRequest) (_result *cloudauth.InitSmartVerifyResponse, _err error) {
	client, _err := CreateClient(endpoint)
	if _err != nil {
		return _result, _err
	}

	// 创建RuntimeObject实例并设置运行参数。
	runtime := &util.RuntimeOptions{}
	runtime.ReadTimeout = tea.Int(10000)
	runtime.ConnectTimeout = tea.Int(10000)

	_body, _err := client.InitSmartVerifyWithOptions(request, runtime)
	if _err != nil {
		return _result, _err
	}
	_result = _body
	return _result, _err
}

func main() {
	err := _main()
	if err != nil {
		panic(err)
	}
}
